// pages/users/index/index.js
const customRequst = require('../../../common/customRequst');
const customUser = require('../../../common/commonUser');
const app = getApp();

Page({
  data: {
    avatarUrl: '/images/user_head.png',
    nickName: '',
    money: '0.00',
    islogin:false,
  },
  onLoad: function (options) {
    let that = this;
  },
  onShow:function(){
    
      app.globalData.Chat.windowsobj = this;
      if (!app.globalData.Chat.serviceConfig.isWebScoket) {
          let userIsLogin = customUser.getUserToken();
          if (userIsLogin) {
              app.globalData.Chat.init();
          }
      }

    let that = this;
    //获取用户信息
    let userInfo = customUser.getUserInfo();
    if(userInfo){
      that.setData({
        avatarUrl: userInfo.avatar,
        nickName: userInfo.nickname,
        islogin:true,
      });
      that.getBalance()
    }
  },
  //获取账户余额信息
  getBalance() {
    let that = this
    if(that.data.islogin){
      customRequst.postRequest('api/accountbalance/recharges').then(res => {
       if(res.code == 1){
        that.setData({
          money: res.data
        })
       }
      })
    }
  },
  goBalance() {
    let that = this;
    if(!that.isUserLogin()){
      return false;
    }
    wx.navigateTo({
      url: '/pages/balance/balance'
    })
  },
  toContribution() {
    let that = this;
    if(!that.isUserLogin()){
      return false;
    }
    wx.navigateTo({
      url: '/pages/order/list/index'
    })
  },
  toRechargesLog:function(){
    let that = this;
    if(!that.isUserLogin()){
      return false;
    }
    wx.navigateTo({
      url: '/pages/users/rechargeslog/index'
    })
  },
  toPromote() {
    let that = this;
    if(!that.isUserLogin()){
      return false;
    }
    wx.navigateTo({
      url: '/pages/promote/promote'
    })
  },
  toUserInfo:function(){
    let that = this;
    if(!that.isUserLogin()){
      return false;
    }
    wx.navigateTo({
      url: '/pages/users/info/index'
    })
  },

  //判断是否登录
  isUserLogin:function(){
    let that = this;
    if(!that.data.islogin){
      wx.showModal({
        title: '登录提示',
        content: '您还未登录，请先登录',
        success: function (res) {
          if (res.confirm) {
            let url = '/pages/users/index/index';
            wx.navigateTo({
              url: '/pages/users/login/index?url='+url
            })
          }
        }
      });
      return false;
    }else{
      return true;
    }
  },

  login:function(){
    let url = '/pages/users/index/index';
    if(customRequst.serverDomain.indexOf('192') > -1){
      console.log('有')
      wx.navigateTo({
        url: '/pages/account/account'
      })
    } else {
      wx.navigateTo({
        url: '/pages/users/login/index?url='+url
      })
    }
  },

  // 退出登录
  loginout() {
    let that = this
    customRequst.postRequest('api/user/logout').then(function(re){
      if(re.code == 1){
        customUser.clearUserInfo();
        wx.showToast({
          title: '退出登录成功',
          duration:2500,
          success:function(){
            that.setData({
              avatarUrl: '/images/user_head.png',
              nickName: '',
              islogin:false,
            });
          }
        })
      }
    })
  },
    handleNewMessage:function(msg){
        wx.showTabBarRedDot({
            index: 2,
        })
    },
    //处理聊天返回的数据
    handleChatMag: function (msg) {
        switch (msg.msgtype) {
            case 'new_message': //收到新消息
                wx.showTabBarRedDot({
                    index: 2,
                });
                break;
            case "push_sys_message":
                wx.showTabBarRedDot({
                    index: 2,
                });
                break;
        }
    },
    //跳转意见反馈
    onProposal:function(){
      wx.navigateTo({
        url: '/pages/users/proposal/index',
      })
    }

})